package xin.tianhui.cloud.weblog.service.impl;

import xin.tianhui.cloud.weblog.model.dto.ChannelAttrResultDTO;
import xin.tianhui.cloud.weblog.model.po.ChannelAttr;
import xin.tianhui.cloud.weblog.mapper.ChannelAttrMapper;
import xin.tianhui.cloud.weblog.service.IChannelAttrService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;

import java.util.Collections;
import java.util.List;
import java.util.Optional;

import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author junchen
 * @since 2018-04-06
 */
@Service
@CacheConfig(cacheNames="channel_attr",keyGenerator="keyGenerator")
public class ChannelAttrServiceImpl extends ServiceImpl<ChannelAttrMapper, ChannelAttr> implements IChannelAttrService {
	
	@Cacheable(key="'channelId:'+#channelId")
	@Override
	public List<ChannelAttrResultDTO> getChannelAttrs(Integer channelId) {
		ChannelAttr attr = new ChannelAttr();
		attr.setChannelId(channelId);
		QueryWrapper<ChannelAttr> wrapper = new QueryWrapper<ChannelAttr>(attr);
		return Optional.ofNullable(baseMapper.selectList(wrapper)).map(list->{
			List<ChannelAttrResultDTO> dtos = Lists.newArrayList();
			list.forEach(r->{
				ChannelAttrResultDTO dto = new ChannelAttrResultDTO();
				dto.setName(r.getName());
				dto.setValue(r.getValue());
				dtos.add(dto);
			});
			return dtos;
		}).orElse(Collections.emptyList());
	}

}
